#include "stm8l15x.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "unit_instance.h"
#include "usart.h"
#include "cmdthread.h"
#include "pubset.h"
#include "cmd_adcoper.h"
#include "adc.h"
#include "formatcmd.h"

extern strRamData RamData;


const struct STADCON stadcon[] = {
  {"pa0", 0},
  {"pa1", 0},
  {"pa2", 0},
  {"pa3", 0},
  {"pa4", ADC_Channel_2},
  {"pa5", ADC_Channel_1},
  {"pa6", ADC_Channel_0},
  {"pa7", 0},
  {"pb0", ADC_Channel_18},
  {"pb1", ADC_Channel_17},
  {"pb2", ADC_Channel_16},
  {"pb3", ADC_Channel_15},
  {"pb4", ADC_Channel_14},
  {"pb5", ADC_Channel_13},
  {"pb6", ADC_Channel_12},
  {"pb7", ADC_Channel_11},
  {"pc0", 0},
  {"pc1", 0},
  {"pc2", ADC_Channel_6},
  {"pc3", ADC_Channel_5},
  {"pc4", ADC_Channel_4},
  {"pc5", 0},
  {"pc6", 0},
  {"pc7", ADC_Channel_3},
  {"pd0", ADC_Channel_22},
  {"pd1", ADC_Channel_21},
  {"pd2", ADC_Channel_20},
  {"pd3", ADC_Channel_19},
  {"pd4", ADC_Channel_10},
  {"pd5", ADC_Channel_9},
  {"pd6", ADC_Channel_8},
  {"pd7", ADC_Channel_7},
  {"pd0", 0},
  {"pd1", 0},
  {"pd2", 0},
  {"pd3", 0},
  {"pd4", 0},
  {"pd5", 0},
  {"pd6", 0},
  {"pd7", 0},
  {"pe0", 0},
  {"pe1", 0},
  {"pe2", 0},
  {"pe3", 0},
  {"pe4", 0},
  {"pe5", ADC_Channel_23},
  {"pe6", 0},
  {"pe7", 0},
  {"pf0", ADC_Channel_24},
  {"pf1", ADC_Channel_25},
  {"pf2", ADC_Channel_26},
  {"pf3", ADC_Channel_27},
  {"pf4", 0},
  {"pf5", 0},
  {"pf6", 0},
  {"pf7", 0},
  {"ph0", 0},
  {"ph1", 0},
  {"ph2", 0},
  {"ph3", 0},
  {"ph4", 0},
  {"ph5", 0},
  {"ph6", 0},
  {"ph7", 0},
  {"pi0", 0},
  {"pi1", 0},
  {"pi2", 0},
  {"pi3", 0},
  {"pi4", 0},
  {"pi5", 0},
  {"pi6", 0},
  {"pi7", 0},
};


char
cmd_adcin (char *s)
{
  u8 i;extern u16 ivddout;
  const char prev[] = "The adcin context adcin pc6 \r\n";
  const char errmsg[] = "no adc chanel \n";
	u16 ivalue;
	u16 itemp;

  for (i = 0; i < sizeof (stadcon) / sizeof (struct STADCON); i++)
    {
      if (strcmp ((char *) _incmdtext1, stadcon[i].spin) == 0)
			break;
    }

  if ((i >= (sizeof (stadcon) / sizeof (struct STADCON))) 
      || (0 == stadcon[i].ichanel))
    {
      printf ("%s\r\n%s\r\n", errmsg,prev);
			return SUCCESS;
    }
 
ivalue = adcstart (stadcon[i].ichanel);
  itemp = vol_format_change( ivalue );
	printf ("The vref=(%d[%04x])", ivddout, ivddout );
  printf (" The chanel %s adcout =0x%04x(%d),vol = %d.%d(v)\n", _incmdtext1, ivalue, ivalue,itemp/1000,itemp%1000);
 
  return SUCCESS;
}
